<?php


namespace app\api\service\cms;

//分词索引
use app\admin\model\cms\IntroduceModel;
use app\admin\model\cms\Platepost;
use app\admin\model\Investment;
use app\api\service\JieBaFenCi;
use app\common\model\Attachment;

class CutWordIndex
{
    //查询文章索引表获取文章id
    public function getCutWordIndex($keyword, $table_name = 'investment', array $where = [])
    {
        $a_id = \app\admin\model\CutWordIndex::where('table_name', $table_name);



        if (!empty($where['aid'])) {
            $a_id = $a_id->whereIn('a_id', $where['aid']);
        }

        $a_id = $a_id->where('key_word', 'like', "%{$keyword}%")->distinct(true)
                ->column('a_id') ?? [];

        return [
            'a_id' => $a_id
        ];
    }


    /**
     * 添加分词到分词索引表
     * @param $aid
     * @param string $table_name
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addCutWordIndex($aid, $table_name = 'investment')
    {
        switch ($table_name) {
            case 'investment':
                $res = $this->addInvestmentIndex($aid, $table_name);
                break;
            case 'introduce':
                $res = $this->addIntroduceIndex($aid, $table_name);
                break;
            case 'plate_post':
                $res = $this->addPlatePostIndex($aid, $table_name);
                break;

        }
        return $res;
    }

    /**
     * 更新索引表分词
     * @param $aid 文章id
     * @param $table_name 分词表的 table_name
     */
    public function updateCutWordIndex($aid, $table_name = 'investment')
    {
        $this->delCutWordIndex($aid, $table_name);
        $this->addCutWordIndex($aid, $table_name);
        return true;
    }

    //

    /**
     * 删除索引表分词
     * @param $aid
     * @param $table_name
     */
    public function delCutWordIndex($aid, $table_name = 'investment')
    {
        $del = \app\admin\model\CutWordIndex::where('table_name', $table_name)->where('a_id', $aid)->delete();
        return true;
    }

    //制造存储数据
    private function createData($aid, $field_name, $str_arr, $table_name = 'investment')
    {
        //$cur_arr = array_unique($str_arr);//去重
        //存入投研日志文章检索索引表
        //$cur_arr = join(' ', $cur_arr);

        $cur_arr = implode('', $str_arr);//不分词语,直接存字符串

        $create_data = [
            'a_id' => $aid,//文章
            'field_name' => $field_name,
            'key_word' => $cur_arr,
            'table_name' => $table_name
        ];
        return $create_data;
    }


    /**
     * 添加分词到分词索引表
     * @param $aid
     * @param string $table_name
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addInvestmentIndex($aid, $table_name = 'investment')
    {
        $jieBaFenCi = new JieBaFenCi();//分词

        $data = Investment::field('id,investment_category_id,macro_content,policy_content,fund_content,emotion_content,expect_content,news_content,file_list')->where('id', $aid)->find();//查询需要分词的文章
        if (!$data) {
            return $data;
        }

        $begin = time();
        $count = 0;
        $create_data = [];
        //dump('---------------' . $v['id']);
        $str_macro_content = strip_tags(($data['macro_content'] ?: ''));
        if ($str_macro_content) {
            $str_arr = $jieBaFenCi->cutStatement($str_macro_content);
            //分好词的一维数组
            if ($str_arr) {
                $create_data[] = $this->createData($data['id'], 'macro_content', $str_arr, $table_name);
            }
        }
        //dump(time() - $begin);

        $str_policy_content = strip_tags(($data['policy_content'] ?: ''));
        if ($str_policy_content) {
            $str_arr = $jieBaFenCi->cutStatement($str_policy_content);
            //分好词的一维数组
            if ($str_arr) {
                $create_data[] = $this->createData($data['id'], 'policy_content', $str_arr, $table_name);
            }
        }
        //dump(time() - $begin);

        $str_fund_content = strip_tags(($data['fund_content'] ?: ''));
        if ($str_fund_content) {
            $str_arr = $jieBaFenCi->cutStatement($str_fund_content);
            //分好词的一维数组
            if ($str_arr) {
                $create_data[] = $this->createData($data['id'], 'fund_content', $str_arr, $table_name);
            }

        }
        //dump(time() - $begin);

        $str_emotion_content = strip_tags(($data['emotion_content'] ?: ''));
        if ($str_emotion_content) {
            $str_arr = $jieBaFenCi->cutStatement($str_emotion_content);
            //分好词的一维数组
            if ($str_arr) {
                $create_data[] = $this->createData($data['id'], 'emotion_content', $str_arr, $table_name);
            }

        }
        //dump(time() - $begin);

        $str_expect_content = strip_tags(($data['expect_content'] ?: ''));
        if ($str_expect_content) {
            $str_arr = $jieBaFenCi->cutStatement($str_expect_content);
            //分好词的一维数组
            if ($str_arr) {
                $create_data[] = $this->createData($data['id'], 'expect_content', $str_arr, $table_name);
            }

        }
        //dump(time() - $begin);

        $str_news_content = strip_tags(($data['news_content'] ?: ''));
        if ($str_news_content) {
            $str_arr = $jieBaFenCi->cutStatement($str_news_content);
            //分好词的一维数组
            if ($str_arr) {
                $create_data[] = $this->createData($data['id'], 'news_content', $str_arr, $table_name);
            }
        }
        //dump(time() - $begin);

        //文件列表
        if ($data['file_list']) {
            $str_file_list = json_decode($data['file_list'], JSON_UNESCAPED_UNICODE);
            $str_file_list = array_column($str_file_list, "name");
            $str_file_list = implode(',', $str_file_list);
            $str_arr = $jieBaFenCi->cutStatement($str_file_list);
            //分好词的一维数组
            if ($str_arr) {
                $create_data[] = $this->createData($data['id'], 'file_list', $str_arr, $table_name);
            }
        }

        //入库分词表
        $res = \app\admin\model\CutWordIndex::insertAll($create_data);
        //$count = $count + $r;
        //dd('入库结果=' . $count);
        return $res;
    }

    //添加分类富文本分词
    public function addIntroduceIndex($aid, $table_name)
    {
        $jieBaFenCi = new JieBaFenCi();//分词

        $data = IntroduceModel::field('id,content,file_img,file_video,file_audio,file_data')->where('id', $aid)->find();//查询需要分词的文章
        if (!$data) {
            return $data;
        }

        $begin = time();
        $count = 0;
        $create_data = [];
        //dump('---------------' . $v['id']);
        $str_content = strip_tags($data['content']);
        if ($str_content) {
            $str_arr = $jieBaFenCi->cutStatement($str_content);
            //分好词的一维数组
            if ($str_arr) {
                $create_data[] = $this->createData($data['id'], 'content', $str_arr, $table_name);
            }
        }
        //dump(time() - $begin);

        $file_list = "{$data['file_img']},{$data['file_video']},{$data['file_audio']},{$data['file_data']}";
        $file_list = explode(',', $file_list);
        $file_list = array_unique($file_list);
        $file_list = array_filter($file_list);

        //文件列表
        if ($file_list) {

            //查附件表获取源文件的名称
            $attachment = Attachment::whereIn('url', $file_list)->distinct(true)->column('filename');
            if ($attachment) {
                $attachment = implode(',', $attachment);
                $str_arr = $jieBaFenCi->cutStatement($attachment);
            }

            //分好词的一维数组
            if (!empty($str_arr)) {
                $create_data[] = $this->createData($data['id'], 'file_list', $str_arr, $table_name);
            }
        }

        //入库分词表
        $res = \app\admin\model\CutWordIndex::insertAll($create_data);
        //$count = $count + $r;
        //dd('入库结果=' . $count);
        return $res;
    }

    //添加债市论坛分词
    public function addPlatePostIndex($aid, $table_name)
    {
        $jieBaFenCi = new JieBaFenCi();//分词

        $data = Platepost::field('id,postname,postcontent,file_img,file_video,file_audio,file_data,accessory')->where('id', $aid)->find();//查询需要分词的文章
        if (!$data) {
            return $data;
        }

        $begin = time();
        $count = 0;
        $create_data = [];
        //dump('---------------' . $v['id']);
        $str_postname = strip_tags($data['postname']);//除字符串中的HTML和PHP标签
        if ($str_postname) {
            $str_arr = $jieBaFenCi->cutStatement($str_postname);
            //分好词的一维数组
            if ($str_arr) {
                $create_data[] = $this->createData($data['id'], 'postname', $str_arr, $table_name);
            }
        }
        //dump(time() - $begin);

        $str_postcontent = strip_tags($data['postcontent']);//除字符串中的HTML和PHP标签
        if ($str_postcontent) {
            $str_arr = $jieBaFenCi->cutStatement($str_postcontent);
            //分好词的一维数组
            if ($str_arr) {
                $create_data[] = $this->createData($data['id'], 'postcontent', $str_arr, $table_name);
            }
        }

        $file_list = "{$data['file_img']},{$data['file_video']},{$data['file_audio']},{$data['file_data']},{$data['accessory']}";
        $file_list = explode(',', $file_list);
        $file_list = array_unique($file_list);
        $file_list = array_filter($file_list);

        //文件列表
        if ($file_list) {

            //查附件表获取源文件的名称
            $attachment = Attachment::whereIn('url', $file_list)->distinct(true)->column('filename');
            if ($attachment) {
                $attachment = implode(',', $attachment);
                $str_arr = $jieBaFenCi->cutStatement($attachment);
            }

            //分好词的一维数组
            if (!empty($str_arr)) {
                $create_data[] = $this->createData($data['id'], 'file_list', $str_arr, $table_name);
            }
        }

        //入库分词表
        $res = \app\admin\model\CutWordIndex::insertAll($create_data);
        //$count = $count + $r;
        //dd('入库结果=' . $count);
        return $res;
    }


}